<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="stylesheet" href="./css/style.css" />
    <title>修改用户信息</title>

    <link
      rel="stylesheet"
      type="text/css"
      href="easyui/1.3.4/themes/default/easyui.css"
    />
    <script type="text/javascript" src="js/axios.js"></script>
    <script type="text/javascript" src="js/jquery-1.8.0.min.js"></script>
    <script
      type="text/javascript"
      src="easyui/1.3.4/jquery.easyui.min.js"
    ></script>
    <script
      type="text/javascript"
      src="easyui/1.3.4/locale/easyui-lang-zh_CN.js"
    ></script>
  </head>

  
  <body>
    <h1>修改用户信息</h1>
    <div class="info">
      <div class="form">
        <div class="left">
          <div class="form-item">
            <div class="label">用户编号</div>
            ：
            <input type="text" id="teacherId" hidden />
          </div>
          <div class="form-item">
            <div class="label">用户名称</div>
            ：
            <input type="text" id="teacherName" />
          </div>
          <div class="form-item">
            <div class="label">部门号</div>
            ：
            <input type="text" id="departmentId" />
          </div>
          <div class="form-item">
            <div class="label">用户职称</div>
            ：
            <input type="text" id="teacherTitle" />
          </div>
          <div class="form-item">
            <div class="label">用户邮箱</div>
            ：
            <input type="text" id="teacherEmail" />
          </div>
          <div class="form-item">
            <div class="label">用户手机号</div>
            ：
            <input type="text" id="teacherPhone" />
          </div>
        </div>
      </div>
      <div class="link" style="align-self: start">
        <div class="link-item" id="send-request">修改</div>
        <div class="link-item" id="reset">重置</div>
      </div>
    </div>
    <script>
      const user = JSON.parse(localStorage.getItem("userinfo"));

      const teacherId = $("#teacherId");
      const teacherName = $("#teacherName");
      const departmentId = $("#departmentId");
      const teacherTitle = $("#teacherTitle");
      const teacherEmail = $("#teacherEmail");
      const teacherPhone = $("#teacherPhone");

      const sendRequest = $("#send-request");
      const reset = $("#reset");

       //记录是否为本人
       let identity=false;

      // 获取查询串中的id
      const id = location.search.split("=")[1];
      axios
        .get("http://localhost:8080/teacher/detail?id=" + id)
        .then(({ data }) => {
          teacherId.val(data.data.teacherId);
          teacherName.val(data.data.teacherName);
          departmentId.val(data.data.departmentId);
          teacherTitle.val(data.data.teacherTitle);
          teacherEmail.val(data.data.teacherEmail);
          teacherPhone.val(data.data.teacherPhone);
          identity=(data.data.teacherName==user.teacherName);
        });
        
        sendRequest.click(function () {
         //只有本人和权限更高能修改信息
        let myrole=user.teacherTitle === "管理员";
        let otherrole=(user.teacherTitle === "管理员"&&(document.querySelector("#teacherTitle").value != "管理员"&&document.querySelector("#teacherTitle").value != "超级管理员"));
        let role=(!myrole||identity||otherrole);
         if(role)
      {
        {
        const teacherId = document.querySelector("#teacherId").value;
        const username = document.querySelector("#teacherName").value;
        const departmentId = document.querySelector("#departmentId").value;
        const teacherTitle = document.querySelector("#teacherTitle").value;
        const teacherEmail = document.querySelector("#teacherEmail").value;
        const teacherPhone = document.querySelector("#teacherPhone").value;
        axios
          .post("http://localhost:8080/teacher/update", {
            teacherId: teacherId,
            teacherName: username,
            departmentId: departmentId,
            teacherTitle: teacherTitle,
            teacherEmail: teacherEmail,
            teacherPhone: teacherPhone,
          })
          .then(({ data }) => {
            if (data.data == null) {
                throw new Error(data.msg);
            }
            alert("修改成功！");
            top.location.reload(true);
          })
          .catch((err) => {
            alert(err);  
          });
         }
         }
         else{
          alert("无权修改他人信息");
          top.location.reload(true);
           }
        });
        
        

      reset.click(function () {
        teacherName.val("");
        departmentId.val("");
        teacherTitle.val("");
        teacherEmail.val("");
        teacherPhone.val("");
      });
    </script>
  </body>
</html>
